import { fileURLToPath, URL } from 'node:url'
import { createHtmlPlugin } from 'vite-plugin-html'
import copy from 'rollup-plugin-copy'
import { defineConfig, loadEnv } from 'vite'
import { visualizer } from 'rollup-plugin-visualizer'
import cesium from 'vite-plugin-cesium'; // 引入插件
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default ({ mode }) => {
  return defineConfig({
    base: loadEnv(mode, process.cwd()).VITE_BASE_PUBLICPATH,
    plugins: [
      vue(),
      cesium(),
      visualizer(),
      createHtmlPlugin({
        minify: true,
        entry: 'src/main.js',
        inject: {
          data: {
            title: '杰能科世车载平台',
          },
        },
      }),
      copy({
        targets: [
          { src: './node_modules/libpag/lib/libpag.wasm', dest: process.env.NODE_ENV === 'production' ? 'dist/' : 'public/' },
        ],
        hook: process.env.NODE_ENV === 'production' ? 'writeBundle' : "buildStart",
      }),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },
    server: {
      https: false,
      host: true,
      proxy: {
        '/focusHz': {
          target: loadEnv(mode, process.cwd()).VITE_BASE_COMSWAT,
          changeOrigin: true,
          secure: false,
          rewrite: (p) => p.replace(/^\/focusHz/, ""),
        },
        '/mapApi': {
          target: loadEnv(mode, process.cwd()).VITE_BASE_MAPURL,
          changeOrigin: true,
          secure: false,
          rewrite: (p) => p.replace(/^\/mapApi/, ""),
        },
      },
    },
  })
}
